package com.zyao89.view.zloading.clock;

import com.zyao89.view.zloading.ZLoadingBuilder;
import com.zyao89.view.zloading.util.Log;
import com.zyao89.view.zloading.util.NumCalcUtil;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

/**
 * Created by zyao89 on 2017/3/19.
 * Contact me at 305161066@qq.com or zyao89@gmail.com
 * For more projects: https://github.com/zyao89
 * My Blog: http://zyao89.me
 */
public class CircleBuilder extends ZLoadingBuilder {
    private static final String TAG = "Zloading CircleBuilder";
    private static final float DEFAULT_ANGLE = -90;
    private RectFloat mInnerCircleRectF;
    private Paint mPaint;
    private float mStartAngle;
    private float mEndAngle;
    private boolean mIsFirstState = true;
    private Arc mInnerCircleArc;
    private float viewCenterX, viewCenterY;


    private void initValues(Context context) {
        float allSize = getAllSize();
        float innerRadius = NumCalcUtil.subtract(allSize, dip2px(context, 3));
        mInnerCircleArc = new Arc();
        mStartAngle = DEFAULT_ANGLE;
        mEndAngle = DEFAULT_ANGLE;
        viewCenterX = getViewCenterX();
        viewCenterY = getViewCenterY();
        mInnerCircleRectF = new RectFloat(NumCalcUtil.subtract(viewCenterX, innerRadius),
            NumCalcUtil.subtract(viewCenterY, innerRadius),
            NumCalcUtil.add(viewCenterX, innerRadius), NumCalcUtil.add(viewCenterY, innerRadius));
    }

    @Override
    protected void initParams(Context context, Color color) {
        mPaint = new Paint();
        mPaint.setColor(color);
        initValues(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        mInnerCircleArc.setArc(mStartAngle, NumCalcUtil.subtract(mEndAngle, mStartAngle), true);
        canvas.drawArc(mInnerCircleRectF, mInnerCircleArc, mPaint);
        canvas.restore();
    }

    @Override
    protected void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
    }

    @Override
    protected void prepareStart(AnimatorValue floatValueAnimator) {

    }

    @Override
    protected void prepareEnd() {

    }

    @Override
    protected void computeUpdateValue(AnimatorValue animation, float animatedValue) {
        if (mIsFirstState) {
            mEndAngle = NumCalcUtil.add(animatedValue * 360, DEFAULT_ANGLE);
        } else {
            mStartAngle = NumCalcUtil.add(animatedValue * 360, DEFAULT_ANGLE);
        }
    }

    @Override
    protected void setColorFilter(ColorFilter colorFilter) {
        mPaint.setColorFilter(colorFilter);
    }

    @Override
    public void onStart(Animator animator) {
        Log.sysI(TAG, "onStart");
        mStartAngle = DEFAULT_ANGLE;
        mEndAngle = DEFAULT_ANGLE;
    }

    @Override
    public void onEnd(Animator animator) {
        Log.sysI(TAG, "onEnd");
        mStartAngle = DEFAULT_ANGLE;
        mEndAngle = DEFAULT_ANGLE;
    }

    @Override
    public void onCancel(Animator animator) {
        Log.sysI(TAG, "onCancel");
        mStartAngle = DEFAULT_ANGLE;
        mEndAngle = DEFAULT_ANGLE;
    }

    @Override
    public void onRepeat(Animator animator) {
        Log.sysI(TAG, "onRepeat");
        mIsFirstState = !mIsFirstState;
        if (mIsFirstState) {
            mStartAngle = DEFAULT_ANGLE;
            mEndAngle = DEFAULT_ANGLE;
        } else {
            mStartAngle = DEFAULT_ANGLE;
            mEndAngle = 360 + DEFAULT_ANGLE;
        }
    }

    protected Paint getPaint() {
        return mPaint;
    }

}
